//
//  WBVisitorView.swift
//  weibo
//
//  Created by 李海超 on 2017/5/23.
//  Copyright © 2017年 lihaichao. All rights reserved.
//

import UIKit

class WBVisitorView: UIView {
    
    //通过字典设置访客视图信息
    var infoDict:[String:String]?{
        didSet{
            guard let imageNanme = infoDict?["imageName"],
                let message = infoDict?["message"]
                else {
                    return
            }
            //首页使用默认设置，不需要改，直接返回
            if imageNanme == "" {
                
                startAnimation()
                return
            }
            
            iconImage.image = UIImage(named: imageNanme)
            titleLable.text = message
            
            //小房子隐藏
            houseImage.isHidden = true
            //遮罩隐藏
            maskIconView.isHidden = true

        }
    }
    
   
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //旋转首页访客视图的圆圈
    private func startAnimation(){
    
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2*M_PI
        
        anim.duration = 15
        
        anim.repeatCount = MAXFLOAT
        
        //使动画跳转界面后回来继续旋转
        anim.isRemovedOnCompletion = false
        
        iconImage.layer.add(anim, forKey: nil)
        
    }
    
    
    
    // MARK: - 私有控件
    
    //图像视图
    private lazy var iconImage:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //遮罩图像，不要使用maskView,UIView有这个属性
    private lazy var maskIconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    
    //小房子
    private lazy var houseImage = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //提示标签
    private lazy var titleLable:UILabel = UILabel.cz_label(withText: "关注一些人，回这里看看有什么惊喜", fontSize: 14, color: UIColor.gray)
    
    //注册按钮
    lazy var registerButton:UIButton = UIButton.cz_textButton(
        "注册",
        fontSize: 16,
        normalColor: UIColor.orange,
        highlightedColor: UIColor.black,
        backgroundImageName: "common_button_white_disable")
    
    //登录按钮
    lazy var loginButton:UIButton = UIButton.cz_textButton(
        "登录",
        fontSize: 16,
        normalColor: UIColor.darkGray,
        highlightedColor: UIColor.black,
        backgroundImageName: "common_button_white_disable")
    


    //创建UI
    func setupUI(){
    
        backgroundColor = UIColor.cz_color(withHex: 0xEDEDED)
       
        //添加控件
        addSubview(iconImage)
        addSubview(maskIconView)
        addSubview(houseImage)
        addSubview(titleLable)
        addSubview(registerButton)
        addSubview(loginButton)
        
        titleLable.textAlignment = .center
        
        //使用纯代码自动布局，要取消autoresizing
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //自动布局
        let margin:CGFloat = 20
        
        
        //图像视图
        addConstraint(NSLayoutConstraint(
            item: iconImage,
            attribute: .centerX,
            relatedBy: .equal,
            toItem: self,
            attribute: .centerX,
            multiplier: 1.0,
            constant: 0))
        
        addConstraint(NSLayoutConstraint(
            item: iconImage,
            attribute: .centerY,
            relatedBy: .equal,
            toItem: self,
            attribute: .centerY,
            multiplier: 1.0,
            constant: -60))
        //小房子
        addConstraint(NSLayoutConstraint(
            item: houseImage,
            attribute: .centerX,
            relatedBy: .equal,
            toItem: iconImage,
            attribute: .centerX,
            multiplier: 1.0,
            constant: 0))
        
        addConstraint(NSLayoutConstraint(
            item: houseImage,
            attribute: .centerY,
            relatedBy: .equal,
            toItem: iconImage,
            attribute: .centerY,
            multiplier: 1.0,
            constant: 0))
        
        //标签lable
        addConstraint(NSLayoutConstraint(
            item: titleLable,
            attribute: .centerX,
            relatedBy: .equal,
            toItem: houseImage,
            attribute: .centerX,
            multiplier: 1.0,
            constant: 0))
        
        addConstraint(NSLayoutConstraint(
            item: titleLable,
            attribute: .centerY,
            relatedBy: .equal,
            toItem: iconImage,
            attribute: .bottom,
            multiplier: 1.0,
            constant: margin))
        
        addConstraint(NSLayoutConstraint(
            item: titleLable,
            attribute: .width,
            relatedBy: .equal,
            toItem: nil,
            attribute: .notAnAttribute,
            multiplier: 1.0,
            constant: 236))
        
        //注册按钮
        addConstraint(NSLayoutConstraint(
            item: registerButton,
            attribute: .left,
            relatedBy: .equal,
            toItem: titleLable,
            attribute: .left,
            multiplier: 1.0,
            constant: 0))
        
        addConstraint(NSLayoutConstraint(
            item: registerButton,
            attribute: .top,
            relatedBy: .equal,
            toItem: titleLable,
            attribute: .bottom,
            multiplier: 1.0,
            constant: margin))
        
        addConstraint(NSLayoutConstraint(
            item: registerButton,
            attribute: .width,
            relatedBy: .equal,
            toItem: nil,
            attribute: .notAnAttribute,
            multiplier: 1.0,
            constant: 100))
        
        //登录按钮
        addConstraint(NSLayoutConstraint(
            item: loginButton,
            attribute: .right,
            relatedBy: .equal,
            toItem: titleLable,
            attribute: .right,
            multiplier: 1.0,
            constant: 0))
        
        addConstraint(NSLayoutConstraint(
            item: loginButton,
            attribute: .top,
            relatedBy: .equal,
            toItem: titleLable,
            attribute: .bottom,
            multiplier: 1.0,
            constant: margin))
        
        addConstraint(NSLayoutConstraint(
            item: loginButton,
            attribute: .width,
            relatedBy: .equal,
            toItem: registerButton,
            attribute: .width,
            multiplier: 1.0,
            constant: 0))
        
        //遮罩图像
        
        //views: VFL中的控件名称和实际对象的映射
        //metrics: VFL中常量映射关系
        
        let metrics = ["spacing":-35]
        
        let viewDict = ["maskIconView":maskIconView,
                        "registerButton":registerButton] as [String : Any]
        
        
        
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[maskIconView]-0-|", options: [], metrics: nil, views: viewDict))
        
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[maskIconView]-(spacing)-[registerButton]", options: [], metrics: metrics, views: viewDict))

    }
}
